package com.yanqu.road.server.manger.player;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eAgentErrorCode;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ChatModule;
import com.yanqu.road.server.manger.ChatControlMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class GameChatControlMgr extends ChatControlMgr {

    @Override
    public boolean reloadData() {
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        reloadChatControl(serverInfo.getChannelId());
        syncOnlineUser();
        return true;
    }

    public static void syncOnlineUser() {
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(ChatModule.class).syncChatControlData();
        }
    }

    public static String getUserChatMessage(JSONObject param) {
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        SortedMap<String, String> paramMap = new TreeMap<>();
        long userId = param.getLong("userId");
        int messageType = param.getInteger("messageType");

        List<JSONObject> chatList = new ArrayList<>();
        if (messageType == eMessageType.WorldChat.getValue()) {
            List<WorldMessageProto.WorldMessageTempMsg.Builder> worldMessageMsgList = WorldMessageMgr.getWorldMessageMsgList();
            for (WorldMessageProto.WorldMessageTempMsg.Builder chatMsg : worldMessageMsgList) {
                if (chatMsg.getUserId() != userId) {
                    continue;
                }
                if (chatMsg.getContentType() == 2) {//表情不需要
                    continue;
                }
                JSONObject chatObject = new JSONObject();
                chatObject.put("type", chatMsg.getType());
                chatObject.put("sendTime", chatMsg.getSendTime());
                chatObject.put("content", chatMsg.getContent());
                chatObject.put("userId", chatMsg.getUserId());
                chatList.add(chatObject);
            }
        } else if (messageType == eMessageType.UnionChat.getValue()) {
            WorldMessageProto.WorldMessageListMsg.Builder respMsg = WorldMessageProto.WorldMessageListMsg.newBuilder();
            CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToCrossServer(userId, CrossProtocol.C_UNION_MEMBER_CHAT_LIST_BY_USERID, null, respMsg);
            try {
                WorldMessageProto.WorldMessageListMsg.Builder builder = future.thenApply((v) -> {
                    if (v.getErrorCode() != 0) {
                        getLogger().info("getunionchatlist response error: {}", v.getErrorCode());
                        return null;
                    }
                    return respMsg;
                }).get();
                if (null != builder) {
                    for (WorldMessageProto.WorldMessageTempMsg chatMsg : builder.getDataListList()) {
                        JSONObject chatObject = new JSONObject();
                        chatObject.put("type", chatMsg.getType());
                        chatObject.put("sendTime", chatMsg.getSendTime());
                        chatObject.put("content", chatMsg.getContent());
                        chatObject.put("userId", chatMsg.getUserId());
                        chatList.add(chatObject);
                    }
                } else {
                    ret.put("content", "等待超时");
                    AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.FunException.getValue());
                    return ret.toString();
                }

            } catch (InterruptedException e) {
                ret.put("content",e.toString());
                AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.FunException.getValue());
                return ret.toString();
            } catch (ExecutionException e) {
                ret.put("content",e.toString());
                AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.FunException.getValue());
                return ret.toString();
            }

        } else if (messageType == eMessageType.CrossGroupChat.getValue()) {
            WorldMessageProto.WorldMessageListMsg.Builder respMsg = WorldMessageProto.WorldMessageListMsg.newBuilder();
            CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToCrossServer(userId, CrossProtocol.C_CROSS_GROUP_CHAT_LIST_BY_USERID, null, respMsg);
            try {
                WorldMessageProto.WorldMessageListMsg.Builder builder = future.thenApply((v) -> {
                    if (v.getErrorCode() != 0) {
                        getLogger().info("getunionchatlist response error: {}", v.getErrorCode());
                        return null;
                    }
                    return respMsg;
                }).get();
                if (null != builder) {
                    for (WorldMessageProto.WorldMessageTempMsg chatMsg : builder.getDataListList()) {
                        JSONObject chatObject = new JSONObject();
                        chatObject.put("type", chatMsg.getType());
                        chatObject.put("sendTime", chatMsg.getSendTime());
                        chatObject.put("content", chatMsg.getContent());
                        chatObject.put("userId", chatMsg.getUserId());
                        chatList.add(chatObject);
                    }
                } else {
                    ret.put("content", "等待超时");
                    AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.FunException.getValue());
                    return ret.toString();
                }

            } catch (InterruptedException e) {
                ret.put("content",e.toString());
                AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.FunException.getValue());
                return ret.toString();
            } catch (ExecutionException e) {
                ret.put("content",e.toString());
                AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.FunException.getValue());
                return ret.toString();
            }
        }
        AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        if (!chatList.isEmpty()) {
            ret.put("data", chatList);
        }
        return ret.toString();
    }
}
